﻿ using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.WXshopBuiness;
using Repository.WXshopBuiness; 
using System;
using System.Collections.Generic;
 

namespace Services.WXshopBuiness
{
   public  class AddressServices:Service
    {
        IRepositoryAsync<Address> m_AddressRepository;        

        public AddressServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_AddressRepository = m_UnitOfWork.RepositoryAsync<Address>();        
        }

        public Address GetById(string Id)
        {
            return m_AddressRepository.GetById(Id);
        }

        public List<Address> GetMyAddress(string userId)
        {
            return m_AddressRepository.GetMyAddress(userId);
        }

        public bool AddAddress(Address ad)
        {
            ad.Enable = 1;
            ad.EntityState = EntityState.Added;
            ad.CreateTime = DateTime.Now;
            m_AddressRepository.Insert(ad);
            return m_UnitOfWork.SaveChanges()==1;
        }

        public bool EditAddress(Address ad)
        {            
            ad.EntityState = EntityState.Modified;
            ad.CreateTime = DateTime.Now;
            m_AddressRepository.Update(ad);
            return m_UnitOfWork.SaveChanges() == 1;
        }

        public bool DelAddress(string addressId)
        {
            Address ad =  m_AddressRepository.GetById(addressId);
            int result=0;
            if (ad != null)
            {
                ad.EntityState = EntityState.Deleted;
                m_AddressRepository.Delete(ad);
                result =  m_UnitOfWork.SaveChanges();
            }
            return result == 1;
        }
    }
}
